package com.luo.partner.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.luo.common.annotation.BaseService;
import com.luo.common.base.BaseServiceImpl;
import com.luo.common.utils.Query;
import com.luo.partner.dao.mapper.TabRedRecordMapper;
import com.luo.partner.dao.mapper.ext.ExtTabRedRecordMapper;
import com.luo.partner.dao.model.TabRed;
import com.luo.partner.dao.model.TabRedRecord;
import com.luo.partner.dao.model.TabRedRecordExample;
import com.luo.partner.dao.model.TabRedRecordTemp;
import com.luo.partner.service.TabRedRecordService;

/**
* TabRedRecordService实现
* Created by laohu on 2018/9/6.
*/
@Service
@Transactional
@BaseService
public class TabRedRecordServiceImpl extends BaseServiceImpl<TabRedRecordMapper, TabRedRecord, TabRedRecordExample> implements TabRedRecordService {

    private static final Logger LOGGER = LoggerFactory.getLogger(TabRedRecordServiceImpl.class);

    @Autowired
    TabRedRecordMapper tabRedRecordMapper;
    @Autowired
    ExtTabRedRecordMapper extTabRedRecordMapper;


    @Override
    public TabRedRecord get(Long id) {
        return tabRedRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TabRedRecord> list() {
        return tabRedRecordMapper.selectByExample(new TabRedRecordExample());
    }

    @Override
    public int save(TabRedRecord role) {
        return tabRedRecordMapper.insert(role);
    }

    @Override
    public int update(TabRedRecord role) {
        return tabRedRecordMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public int remove(Long id) {
        return tabRedRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TabRedRecord> list(Long userId) {
    	TabRedRecordExample example = new TabRedRecordExample();
    	example.or().andUsrsIdEqualTo(userId);
        return tabRedRecordMapper.selectByExample(example);
    }

    @Override
    public int batchRemove(Long[] ids) {
        return 0;
    }

    @Override
    public int count(Query query) {
        return 0;
    }
    
    @Override
    public int addUserRed(Long usrsId, TabRed tabRed,BigDecimal un_settlement_recharge) {
    	if (tabRed.getSetType() == 1) {
			//1表示按充值金额的百分比
    		TabRedRecord record = new TabRedRecord();
        	record.setRedId(tabRed.getRedId());
        	record.setState(0);
        	record.setUsrsId(usrsId);
        	record.setAmount(tabRed.getRate().divide(new BigDecimal(100)).multiply(un_settlement_recharge));
        	record.setDescription(tabRed.getDescription());
        	record.setRedName(tabRed.getRedName());
        	record.setCreateTime(new Date());
        	return tabRedRecordMapper.insertSelective(record);
		}else if (tabRed.getSetType() == 0){
			//0表示按上下限
			TabRedRecord record = new TabRedRecord();
	    	record.setRedId(tabRed.getRedId());
	    	record.setState(0);
	    	record.setUsrsId(usrsId);
	    	record.setAmount(tabRed.getAmount());
	    	record.setDescription(tabRed.getDescription());
	    	record.setRedName(tabRed.getRedName());
        	record.setCreateTime(new Date());
	    	return tabRedRecordMapper.insertSelective(record);
		}
    	return 0;
    }

	@Override
	public List<TabRedRecordTemp> findRedList(Map<String, Object> map) {
		return extTabRedRecordMapper.findRedList(map);
	}

	@Override
	public int countRedList(Map<String, Object> map) {
		return extTabRedRecordMapper.countRedList(map);
	}
}